import BaseIndex from './baseIndex';
import { Common } from 'UtilsCommon';

export default class property extends BaseIndex {
    constructor() {
        super();
        this.initExpand("property");
    }

    setIsVisible(props, action) {
        const { pageAxis } = props;
        const { targetPropertyName, isVisible } = action;
        if (!action.targetProperty) action.targetProperty = pageAxis.getProperty(targetPropertyName);
        if (!action.targetProperty) return;

        if (action.targetProperty.setIsVisible) action.targetProperty.setIsVisible(isVisible);
        action.targetProperty.isVisible = isVisible;
    }

    setShowHide(props, action) {
        const { pageAxis } = props;
        const { showPropertyNames, hidePropertyNames } = action;

        showPropertyNames.split(',').forEach(n => {
            const p = pageAxis.getProperty(n);
            if (p) {
                const ele = p.getElement ? p.getElement() : null;
                if (action.isElementVisible && ele) Common.setElementVisible(ele, true);
                else if (p.setIsVisible) p.setIsVisible(true);
                p.isVisible = true;
            }
        });

        hidePropertyNames.split(',').forEach(n => {
            const p = pageAxis.getProperty(n);
            if (p) {
                const ele = p.getElement ? p.getElement() : null;
                if (action.isElementVisible && ele) Common.setElementVisible(ele, false);
                else if (p.setIsVisible) p.setIsVisible(false);
                p.isVisible = false;
            }
        });

        if (action.expandSet && pageAxis[action.expandSet]) pageAxis[action.expandSet](action);
    }

    playVideo(props, action) {
        const { pageAxis, property } = props;
        const { videoPlayerName, videoUrl } = action;
        if (!action.videoPlayer) action.videoPlayer = pageAxis.getProperty(videoPlayerName);
        if (!action.videoPlayer) return;

        if (action.videoPlayer.setVideoSrc) {
            action.videoPlayer.setVideoSrc(videoUrl, () => {
                property.setIsVisible(true);
            });
            property.setIsVisible(false);
        }
        if (action.actionId && window.a2st) window.a2st.addAction(action.actionId);
    }
}